package com.example.aline.tracedrawdemo;

import android.content.res.AssetManager;

import com.amap.api.maps.model.LatLng;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liguoying on 2017/8/15.
 */

public class TraceAsset {
    public static List<LatLng> parseLocationsData(
            AssetManager mAssetManager, String filePath) {
        List<LatLng> locLists = new ArrayList<LatLng>();
        InputStream input = null;
        InputStreamReader inputReader = null;
        BufferedReader bufReader = null;
        try {
            input = mAssetManager.open(filePath);
            inputReader = new InputStreamReader(input);
            bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                String[] strArray = null;
                strArray = line.split(",");
                LatLng newpoint = new LatLng(Double.parseDouble(strArray[1]), Double.parseDouble(strArray[2]));
                if (locLists.size() == 0 || newpoint.toString() != locLists.get(locLists.size() - 1).toString()) {
                    locLists.add(newpoint);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufReader != null) {
                    bufReader.close();
                    bufReader = null;
                }
                if (inputReader != null) {
                    inputReader.close();
                    inputReader = null;
                }
                if (input != null) {
                    input.close();
                    input = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return locLists;
    }

}
